//
//  YSZStatus.swift
//  swiftweibo6
//
//  Created by ysz on 15/11/29.
//  Copyright © 2015年 user. All rights reserved.
//

import UIKit

class YSZStatus: NSObject
{
    ///微博时间
    var created_at: String?
    
    ///微博id
    var id: Int = 0
    
    ///微博内容
    var text: String?
    
    ///微博来源
    var source: String?
    
    ///微博缩略图 图片地址数组：数组里面放的是字典
    var pic_urls: [[String: AnyObject]]?
    
    ///
    var user: YSZUser?
    
    ///转发数
    var reposts_count: Int = 0
    
    ///评论数
    var comments_count: Int = 0
    
    ///赞
    var attitudes_count: Int = 0
    
    ///字典转模型
    init(dict: [String: AnyObject])
    {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user"
        {
            if let dict = value as? [String: AnyObject]
            {
                user = YSZUser(dict: dict)
            }
            //千万要记得return,不然会被父类从新赋值,覆盖掉了
            return
        }
        super.setValue(value, forKey: key)
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    class func loadStatus(finished: (statuses: [YSZStatus]?, error:NSError?)-> ())
    {
        YSZNetworkTools.shardInsatance.loadStatus { (result, error) -> () in
            if error != nil || result == nil
            {
                print("\(error)")
                finished(statuses: nil, error: error)
                return
            }
            //加载到微博数据，字典转模型
            if let array = result!["statuses"] as? [[String: AnyObject]]
            {
                var statuses = [YSZStatus]()
                
                for dict in array
                {
                    let status = YSZStatus(dict: dict)
                    statuses.append(status)
                }
                finished(statuses: statuses, error: nil)
            }
            else
            {
                // 返回的数据不能字典转模型,告诉调用者,没有模型数据
                finished(statuses: nil, error: nil)
            }
            
        }
    }
    
}
